package com.baomidou.mybatisplus.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.util.Collections;

/**
 * @Author: 曾睿
 * @Date: 2020/5/22 17:02
 */
public class CodeGenerator {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/mybatis-plus-test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "root";
        String author = "Jerry.Zeng";
        String table = "users";

        FastAutoGenerator.create(url, username, password)
                // 全局策略
                .globalConfig(builder -> builder
                        .fileOverride()
                        .disableOpenDir()
                        // 输出目录
                        .outputDir("D:/code/mybatis-plus/src/main/java")
                        .author(author)
                        .enableSwagger()
                        .dateType(DateType.TIME_PACK)
                        .commentDate("yyyy-MM-dd"))
                // 包策略
                .packageConfig(builder -> builder
                        .parent("com.baomidou.mybatisplus.generator") // 所在包名
                        // .moduleName("sys")
                        .entity("domain")
                        .service("service")
                        .serviceImpl("service")
                        .mapper("mapper")
                        // .mapperXml("mapper.xml")
                        .controller("controller")
                        .other("other")
                        // xml目录
                        .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\code\\mybatis-plus\\src\\main\\resources\\mapper\\generator")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder -> builder
                        // .enableSkipView()
                        .disableSqlFilter()
                        .addInclude(table)
                        // ################### domain层策略 #####################
                        .entityBuilder()
                        // .disableSerialVersionUID()
                        // .superClass(BaseEntity.class) // 设置父类
                        .enableLombok()
                        // .enableTableFieldAnnotation() // 生成注解
                        // .enableActiveRecord() // 开启 ActiveRecord 模型
                        // .versionColumnName("version") // 乐观锁字段名(数据库)
                        // .versionPropertyName("version") // 乐观锁属性名(实体)
                        // .logicDeleteColumnName("deleted") // 逻辑删除字段名(数据库)
                        // .logicDeletePropertyName("deleted") // 逻辑删除属性名(实体)
                        // .addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")//添加父类公共字段
                        // .addIgnoreColumns("age") // 添加忽略字段
                        .addTableFills(new Column("open_id", FieldFill.INSERT))
                        .addTableFills(new Column("create_time", FieldFill.INSERT)) //添加表字段填充
                        .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE)) //添加表字段填充
                        .idType(IdType.AUTO) // id自增模式

                        // ################### controller层策略 #####################
                        .controllerBuilder()
                        // .superClass(BaseController.class)
                        .enableHyphenStyle()
                        .enableRestStyle() // 生成@RestController注解

                        // ################### service层策略 ###################
                        .serviceBuilder()
                        // .superServiceClass(BaseService.class)
                        // .superServiceImplClass(BaseServiceImpl.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImp"))
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
